﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Windows;


namespace GUI.BaoCao.BC_Cong_No_NCC
{
    public partial class frmTongHopCongNoNCC : Form
    {
        #region Cap nhap giao diện main
        public delegate void GetBoolean(Boolean capnhat);
        public GetBoolean GetData;
        #endregion

        #region khởi tạo biến
        private Boolean CheckClose = false;
        DateTime date;
        DateTime batdau;
        DateTime ketthuc;
        Entities.SelectTongHopCongNo tem;
        Entities.KhoHang[] khohang;
        Entities.SoDuCongNo[] soducongno;
        Entities.NhaCungCap[] nhacungcap;
        Entities.NhaCungCap ncc;
        Entities.KhoHang khoh;
        ArrayList array;
        ArrayList array1;
        string makho = "";
        string tenkho = "";
        string madiadiem = "";
        Entities.BCTongHopCongNoNCC[] hienthi;
        Entities.BCTongHopCongNoNCC[] baocao;
        Entities.BCTongHopCongNoNCC save;
        System.Windows.Forms.SaveFileDialog savefile;
        int i = 0;
        #endregion
        public frmTongHopCongNoNCC()
        {
            InitializeComponent();
            try
            {
                try
                {
                    Entities.CaiDatKho kho = GUI.Entitystatic.ViewFile();
                    makho = kho.MaKho;
                    tenkho = kho.TenKho;
                    if (makho.Length == 0 || makho == "NULL")
                    {
                        makho = tenkho = "";
                    }
                }
                catch
                {
                }
                date = Common.StaticVariable.InterfaceObject.SelectDate();
                batdau = ketthuc = date;
                SelectData();
                this.HienThiListview();
                comboBox1.Text = tenkho;
                savefile = new System.Windows.Forms.SaveFileDialog();
            }
            catch
            {
            }
        }

        void SelectData()
        {
            tem = Common.StaticVariable.InterfaceObject.SelectTongHopCongNoNCC("TongHopNCC");
            soducongno = Common.StaticVariable.InterfaceObject.SelectCongNoTheoKho(makho);
            if (soducongno == null)
                soducongno = new Entities.SoDuCongNo[0];
            nhacungcap = Common.StaticVariable.InterfaceObject.SelectNhaCungCap(ncc);
            if (nhacungcap == null)
                nhacungcap = new Entities.NhaCungCap[0];

            khohang = Common.StaticVariable.InterfaceObject.SelectKhoHang(khoh);
            if (khohang == null)
                khohang = new Entities.KhoHang[0];
            Entities.KhoHang[] temp = new Entities.KhoHang[khohang.Length + 1];
            if (temp.Length != 1)
            {
                int sotang = 0;
                foreach (Entities.KhoHang item in khohang)
                {
                    temp[sotang] = item;
                    sotang++;
                }
                temp[temp.Length - 1] = new Entities.KhoHang();
                temp[temp.Length - 1].MaKho = "Tất cả";
                temp[temp.Length - 1].TenKho = "Tất cả";
                Common.Utilities.XuLyComboBox(comboBox1, temp, Common.Constants.KhoHang.TenKho, Common.Constants.KhoHang.MaKho);
            }
            else
            {
                Common.Utilities.XuLyComboBox(comboBox1, khohang, Common.Constants.KhoHang.TenKho, Common.Constants.KhoHang.MaKho);
            }
        }

        private void HienThiListview()
        {
            try
            {
                if (hienthi == null)
                {
                    Entities.BCTongHopCongNoNCC[] nv = new Entities.BCTongHopCongNoNCC[0];
                    dgvHienThi.DataSource = nv;
                }
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }
                dgvHienThi.Columns["maNCC"].HeaderText = "Mã NCC";
                dgvHienThi.Columns["maNCC"].Visible = true;
                dgvHienThi.Columns["tenNCC"].HeaderText = "Tên NCC ";
                dgvHienThi.Columns["tenNCC"].Visible = true;
                dgvHienThi.Columns["duNoDauKy"].HeaderText = "Dư Đầu Kỳ";
                dgvHienThi.Columns["duNoDauKy"].Visible = true;
                dgvHienThi.Columns["phatSinhTang"].HeaderText = "Phát sinh nợ";
                dgvHienThi.Columns["phatSinhTang"].Visible = true;
                dgvHienThi.Columns["phatSinhGiam"].HeaderText = "Phát sinh có";
                dgvHienThi.Columns["phatSinhGiam"].Visible = true;
                dgvHienThi.Columns["duNoCuoiKy"].HeaderText = "Dư cuối kỳ";
                dgvHienThi.Columns["duNoCuoiKy"].Visible = true;
                if (dgvHienThi.RowCount > 0)
                {
                    Common.Utilities.CountDatagridview(dgvHienThi);
                    dgvHienThi.Focus();
                    dgvHienThi.Select();
                }
                dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }

        #region In
        void In()
        {
            if (hienthi.Length - 1 == 0)
            {
                return;
            }
            try
            {
                Entities.BCTongHopCongNoNCC[] temp = new Entities.BCTongHopCongNoNCC[hienthi.Length - 1];
                int sotang = 0;
                for (int j = 0; j < hienthi.Length - 1; j++)
                {
                    temp[sotang] = hienthi[j];
                    sotang++;
                }
                BaoCao.frmBaoCaorpt frm = new frmBaoCaorpt(temp, makho, batdau, ketthuc);
                if (frm.kiemtra)
                {
                    frm.ShowDialog();

                }
            }
            catch
            {
            }
        }
        #endregion

        #region xử lý
        void XuLy()
        {
            try
            {
                int sotang = 0;
                array = new ArrayList();
                double sddk, sdck, phatsinhno, phatsinhco;
                double sddk1, sdck1, phatsinhno1, phatsinhco1;
                sdck1 = sddk1 = phatsinhco1 = phatsinhno1 = 0;
                foreach (Entities.NhaCungCap item in nhacungcap)
                {
                    sdck = sddk = phatsinhco = phatsinhno = 0;

                    foreach (Entities.SoDuCongNo item1 in soducongno)
                    {
                        int month = item1.NgayKetChuyen.Month;
                        int year = item1.NgayKetChuyen.Year;
                        if (item1.LoaiDoiTuong == true && month == date.Month && year == date.Year)
                        {
                            if (item.MaNhaCungCap == item1.MaDoiTuong)
                            {
                                sddk = double.Parse(item1.SoDuDauKy);
                                sddk1 += double.Parse(item1.SoDuDauKy);
                                break;
                            }
                        }
                    }

                    // hóa đơn nhập
                    foreach (Entities.HoaDonNhap item1 in tem.HoaDonNhap)
                    {
                        if (comboBox1.SelectedIndex == khohang.Length)
                        {
                            if (item1.MaNhaCungCap == item.MaNhaCungCap && item1.NgayNhap.Date >= batdau.Date && item1.NgayNhap.Date <= ketthuc.Date)
                            {
                                phatsinhco += double.Parse(item1.TongTien);
                                phatsinhco1 += double.Parse(item1.TongTien);
                            }
                        }
                        else
                        {
                            if (item1.MaKho == makho && item1.MaNhaCungCap == item.MaNhaCungCap && item1.NgayNhap.Date >= batdau.Date && item1.NgayNhap.Date <= ketthuc.Date)
                            {
                                phatsinhco += double.Parse(item1.TongTien);
                                phatsinhco1 += double.Parse(item1.TongTien);
                            }
                        }
                    }

                    // trả lại nhà cung câp

                    foreach (Entities.TraLaiNCC item1 in tem.TraLaiNCC)
                    {
                        if (comboBox1.SelectedIndex == khohang.Length)
                        {
                            if (item1.MaNCC == item.MaNhaCungCap && item1.Ngaytra.Date >= batdau.Date && item1.Ngaytra.Date <= ketthuc.Date)
                            {

                                phatsinhno += double.Parse(item1.TienBoiThuong);
                                phatsinhno1 += double.Parse(item1.TienBoiThuong);
                            }
                        }
                        else
                        {
                            if (item1.MaKho == makho && item1.MaNCC == item.MaNhaCungCap && item1.Ngaytra.Date >= batdau.Date && item1.Ngaytra.Date <= ketthuc.Date)
                            {

                                phatsinhno += double.Parse(item1.TienBoiThuong);
                                phatsinhno1 += double.Parse(item1.TienBoiThuong);
                            }
                        }
                    }

                    // phiếu thanh toán của nhà cung câp

                    foreach (Entities.PhieuTTNCC item1 in tem.PhieuTTNCC)
                    {
                            if (item1.MaNhaCungCap == item.MaNhaCungCap && item1.NgayLap.Date >= batdau.Date && item1.NgayLap.Date <= ketthuc.Date)
                            {
                                if (item1.LoaiPhieu == false)
                                {
                                    phatsinhco += double.Parse(item1.TongThanhToan);
                                    phatsinhco1 += double.Parse(item1.TongThanhToan);
                                }
                                else
                                {
                                    phatsinhno += double.Parse(item1.TongThanhToan);
                                    phatsinhno1 += double.Parse(item1.TongThanhToan);
                                }
                            }
                        
                        else
                        {
                            if (item1.MaNhaCungCap == item.MaNhaCungCap && item1.NgayLap.Date >= batdau.Date && item1.NgayLap.Date <= ketthuc.Date)
                            {
                                if (item1.LoaiPhieu == false)
                                {
                                    phatsinhco += double.Parse(item1.TongThanhToan);
                                    phatsinhco1 += double.Parse(item1.TongThanhToan);
                                }
                                else
                                {
                                    phatsinhno += double.Parse(item1.TongThanhToan);
                                    phatsinhno1 += double.Parse(item1.TongThanhToan);
                                }
                            }
                        }
                    }
                    sdck = sddk - phatsinhno + phatsinhco;
                    sdck1 = sddk1 - phatsinhno1 + phatsinhco1;
                    Entities.BCTongHopCongNoNCC temp = new Entities.BCTongHopCongNoNCC();
                    temp.DuNoCuoiKy = double.Parse(Common.Utilities.FormatMoney(sdck.ToString()));
                    temp.DuNoDauKy = double.Parse( Common.Utilities.FormatMoney(sddk.ToString()));
                    temp.MaNCC = item.MaNhaCungCap;
                    temp.PhatSinhGiam = double.Parse( Common.Utilities.FormatMoney(phatsinhco.ToString()));
                    temp.PhatSinhTang = double.Parse( Common.Utilities.FormatMoney(phatsinhno.ToString()));
                    temp.GiaTriCK =  Common.Utilities.FormatMoney(temp.DuNoCuoiKy.ToString());
                    temp.GiaTriDK = ( Common.Utilities.FormatMoney(temp.DuNoDauKy.ToString()));
                    temp.GiaTriPSG = ( Common.Utilities.FormatMoney(temp.PhatSinhGiam.ToString()));
                    temp.GiaTriPST = ( Common.Utilities.FormatMoney(temp.PhatSinhTang.ToString()));
                    temp.STT = sotang + 1;
                    temp.TenNCC = item.TenNhaCungCap;
                    array.Add(temp);
                    sotang++;
                }


                hienthi = new Entities.BCTongHopCongNoNCC[array.Count + 1];
                sotang = 0;

                foreach (Entities.BCTongHopCongNoNCC item in (Entities.BCTongHopCongNoNCC[])array.ToArray(typeof(Entities.BCTongHopCongNoNCC)))
                {
                    hienthi[sotang] = item;
                    sotang++;
                }
                dgvHienThi.DataSource = hienthi;
                hienthi[hienthi.Length - 1] = new Entities.BCTongHopCongNoNCC();
                hienthi[hienthi.Length - 1].TenNCC = "Tổng Cộng";
                hienthi[hienthi.Length - 1].GiaTriDK = ( Common.Utilities.FormatMoney(sddk1.ToString()));
                hienthi[hienthi.Length - 1].GiaTriCK = ( Common.Utilities.FormatMoney(sdck1.ToString()));
                hienthi[hienthi.Length - 1].GiaTriPSG = ( Common.Utilities.FormatMoney(phatsinhco1.ToString()));
                hienthi[hienthi.Length - 1].GiaTriPST = ( Common.Utilities.FormatMoney(phatsinhno1.ToString()));
                save = null;
            }
            catch
            {
            }
        }
        #endregion

        #region Lọc điều kiện
        void LocDieuKien()
        {
            try
            {
                GUI.LocDieuKien.frmLocDieuKien frm = new LocDieuKien.frmLocDieuKien();
                frm.ShowDialog();
                if (GUI.LocDieuKien.frmLocDieuKien.kiemtra)
                {
                    DateTime truoc1;
                    DateTime sau1;
                    try
                    {
                        truoc1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.truoc);
                        sau1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.sau);
                    }
                    catch
                    {
                        return;
                    }
                    batdau = truoc1;
                    ketthuc = sau1;
                    XuLy();
                    GUI.LocDieuKien.frmLocDieuKien.kiemtra = false;
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Word
        void Word()
        {
            if (hienthi.Length - 1 == 0)
            {
                return;
            }
            try
            {
                savefile.Filter = "Word |*.doc"; savefile.FileName = "";
                if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Entities.BCTongHopCongNoNCC[] temp = new Entities.BCTongHopCongNoNCC[hienthi.Length - 1];
                    int sotang = 0;
                    for (int j = 0; j < hienthi.Length - 1; j++)
                    {
                        temp[sotang] = hienthi[j];
                        sotang++;
                    }
                    BaoCao.frmBaoCaorpt frm = new frmBaoCaorpt(temp, makho, batdau, ketthuc, savefile.FileName, Common.Constants.Word);
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Excel
        void Excel()
        {
            if (hienthi.Length - 1 == 0)
            {
                return;
            }
            try
            {
                savefile.Filter = "Excel |*.xls"; savefile.FileName = "";
                if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Entities.BCTongHopCongNoNCC[] temp = new Entities.BCTongHopCongNoNCC[hienthi.Length - 1];
                    int sotang = 0;
                    for (int j = 0; j < hienthi.Length - 1; j++)
                    {
                        temp[sotang] = hienthi[j];
                        sotang++;
                    }
                    BaoCao.frmBaoCaorpt frm = new frmBaoCaorpt(temp, makho, batdau, ketthuc, savefile.FileName, Common.Constants.Excel);
                }
            }
            catch
            {
            }
        }
        #endregion

        #region PDF
        void PDF()
        {
            if (hienthi.Length - 1 == 0)
            {
                return;
            }
            try
            {
                savefile.Filter = "PDF |*.pdf"; savefile.FileName = "";
                if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Entities.BCTongHopCongNoNCC[] temp = new Entities.BCTongHopCongNoNCC[hienthi.Length - 1];
                    int sotang = 0;
                    for (int j = 0; j < hienthi.Length - 1; j++)
                    {
                        temp[sotang] = hienthi[j];
                        sotang++;
                    }
                    BaoCao.frmBaoCaorpt frm = new frmBaoCaorpt(temp, makho, batdau, ketthuc, savefile.FileName, Common.Constants.PDF);
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Thoát
        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        this.CheckClose = true;
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion

        private void btnin_Click(object sender, EventArgs e)
        {
            In();
        }

        private void btnloc_Click(object sender, EventArgs e)
        {
            LocDieuKien();
        }

        private void btnword_Click(object sender, EventArgs e)
        {
            Word();
        }

        private void btnExcel_Click(object sender, EventArgs e)
        {
            Excel();
        }

        private void btnpdf_Click(object sender, EventArgs e)
        {
            PDF();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        private void frmTongHopCongNoNCC_Load(object sender, EventArgs e)
        {

        }
    }
}
